#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project:     pc
# @File:        u_text.py
# @Author:      Fan GuiChuan
# @Date:        2024/7/24 14:17
# @Version:     Python3.7
# @Description: 文本类工具实现文件

class TextTool(object):
    """
    文本类工具类
    """
    # 字数映射，2位为十，3位为百
    __num_count_map = {2: '十', 3: '百', 4: '千', 5: '万', 6: '十', 7: '百', 8: '千', 9: '亿'}
    __num_map = {'0': '零', '1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八', '9': '九'}

    @classmethod
    def trans_num_to_chinese(cls, num: str, is_first=True):
        """
        将数字翻译成中文形式，如一百二十
        :param num:
        :param is_first:
        :return:
        """
        num_ch = ''
        num_count = len(num)
        if num_count > 2:
            if num[0] != '0' and num[1] != '0':
                num_ch += cls.__num_map.get(num[0])
                num_ch += cls.__num_count_map.get(num_count)
                num_ch += cls.trans_num_to_chinese(num[1:], is_first=False)
            elif num[0] != '0' and num[1] == '0':
                num_ch += cls.__num_map.get(num[0])
                num_ch += cls.__num_count_map.get(num_count)
                num = num[1:]
                while len(num) and num[0] == '0':
                    num = num[1:]
                if num:
                    num_ch += cls.__num_map.get('0')
                    num_ch += cls.trans_num_to_chinese(num, is_first=False)
        elif num_count == 2:
            if num[0] == '1' and num[1] != '0' and is_first:
                num_ch += cls.__num_count_map.get(num_count)
                num_ch += cls.trans_num_to_chinese(num[1:])
            elif num[0] == '1' and num[1] == '0' and is_first:
                num_ch += cls.__num_count_map.get(num_count)
                num = num[1:]
                while len(num) and num[0] == '0':
                    num = num[1:]
                if num:
                    num_ch += cls.__num_map.get('0')
                    num_ch += cls.trans_num_to_chinese(num, is_first=False)
            elif num[0] != '0' and num[1] != '0':
                num_ch += cls.__num_map.get(num[0])
                num_ch += cls.__num_count_map.get(num_count)
                num_ch += cls.trans_num_to_chinese(num[1:], is_first=False)
            elif num[0] != '0' and num[1] == '0':
                num_ch += cls.__num_map.get(num[0])
                num_ch += cls.__num_count_map.get(num_count)
                num = num[1:]
                while len(num) and num[0] == '0':
                    num = num[1:]
                if num:
                    num_ch += cls.__num_map.get('0')
                    num_ch += cls.trans_num_to_chinese(num, is_first=False)
        elif num_count == 1:
            if num_ch and num[0] != '0':
                num_ch += cls.__num_map.get(num[0])
            elif not num_ch:
                num_ch = cls.__num_map.get(num[0])

        return num_ch


def test_num_to_chinese(num: str = "0"):
    int_num = int(num)
    ch = TextTool.trans_num_to_chinese(str(int_num))
    print(f"{num} is {ch}")


if __name__ == '__main__':
    test_num_to_chinese("000")
    test_num_to_chinese("010")
    test_num_to_chinese("0110")
    test_num_to_chinese("0")
    test_num_to_chinese("1")
    test_num_to_chinese("10")
    test_num_to_chinese("12")
    test_num_to_chinese("100")
    test_num_to_chinese("101")
    test_num_to_chinese("110")
    test_num_to_chinese("111")
    test_num_to_chinese("1000")
    test_num_to_chinese("1001")
    test_num_to_chinese("1010")
    test_num_to_chinese("1100")
    test_num_to_chinese("1101")
    test_num_to_chinese("1110")
    test_num_to_chinese("1111")
    test_num_to_chinese("102343456")
